/**
 * 
 */
package xiaojian.toolkit.ebus.unit;

import java.util.HashMap;
import java.util.Map;

import xiaojian.toolkit.util.Pair;

/**
 * @author Marvin.Ma
 *
 */
public class CompositeUnitBuilder implements UnitBuilder {

	private	Map<String, UnitBuilder> builders = 
		new HashMap<String, UnitBuilder>();
	
	
	public void setBuilders(Map<String, UnitBuilder> builders) {
        this.builders.clear();
        
        for (Map.Entry<String, UnitBuilder> entry : builders.entrySet()) {
            if ( null != entry.getValue() ) {
                this.builders.put(entry.getKey(), entry.getValue());
            }
        }
	}
	
	/**
	 * @see stc.skymobi.ebus.unit.UnitBuilder#createUnit(stc.skymobi.ebus.unit.UnitSource)
	 */
	public Pair<EventUnit, String> createUnit(UnitSource source) {
		if ( null == source ) {
			return	Pair.of(null, "unit source is null");
		}
		UnitBuilder builder = builders.get( source.getType() );
		if ( null == builder ) {
			return	Pair.of(null, "can't found unit builder for type [" + source.getType() + "]");
		}
		return builder.createUnit(source);
	}

}
